package com.supostacks.xuetangerp.controller;

import cn.dev33.satoken.util.SaResult;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.supostacks.rpc.server.ICardKeyExclusiveService;
import com.supostacks.rpc.server.dto.BaseDto;
import com.supostacks.rpc.server.dto.CardKeyDto;
import com.supostacks.rpc.server.req.FetchReq;
import com.supostacks.rpc.server.res.XResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/card")
public class CardKeyController extends BaseController {

    @DubboReference
    ICardKeyExclusiveService cardKeyExclusiveService;

    @Override
    public BaseDto buildData(JSONObject json) {
        return JSONObject.toJavaObject(json, CardKeyDto.class);
    }

    @Override
    public BaseDto takeBaseDto() {
        return new CardKeyDto();
    }


    @GetMapping("/generate/key")
    public SaResult generateKey() {
        String limitString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789qwertyuiopasdfghjklzxcvbnm";
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 24; i++) {
            int number = (int) (Math.random() * limitString.length());
            sb.append(limitString.charAt(number));
        }
        return SaResult.data(sb.toString());
    }

    @RequestMapping("/generate")
    public SaResult generateCard(@RequestBody JSONObject json) {
        String type = json.getString("type");//卡密类型
        Integer count = json.getInteger("count");//生成数量
        Map<String, String> getenv = System.getenv();
        if (getenv.containsKey("SECRETKEY")) {
            XResult secretkey = cardKeyExclusiveService.generateCard(count, type, getenv.get("SECRETKEY"));
            return SaResult.data(secretkey.getResult()).setCode(secretkey.getCode()).setMsg(secretkey.getMsg());
        }
        return SaResult.error("请先生成一个秘钥，配置环境变量后再生成卡密");
    }

    @PostMapping("/get/code")
    public SaResult getCodeKey(String id) {
        String[] ids = id.split(";");
        CardKeyDto cardKeyDto = new CardKeyDto();
        JSONArray jsonArray = new JSONArray();
        for (String _id : ids) {
            JSONObject json = new JSONObject();
            json.put("id", _id);

            FetchReq req = FetchReq.builder()
                    .option(json)
                    .data(cardKeyDto).type("selectById").build();

            XResult fetch = dataFetchService.fetch(req);
            Object result = fetch.getResult();
            jsonArray.add(result);
        }
        return SaResult.data(jsonArray);
    }


    @RequestMapping("/activate/code")
    public SaResult activateCode(@RequestParam("code") String code,
                                @RequestParam("openid") String openid) {
        try {
            Map<String, String> getenv = System.getenv();
            if (getenv.containsKey("SECRETKEY")) {
                String secretKey = getenv.get("SECRETKEY");
                XResult xResult = cardKeyExclusiveService.activateCode(code, secretKey,openid);
                return SaResult.data(xResult.getResult()).setCode(xResult.getCode()).setMsg(xResult.getMsg());
            }
            return SaResult.error("请先生成一个秘钥，配置环境变量后再生成卡密");
        } catch (Exception e) {
            return SaResult.error("卡密无效");
        }
    }
}
